var contentPanel = new Ext.TabPanel({
	region : 'center',
	enableTabScroll : true,
	activeTab : 0,
	items : [{
		id : 'homePage',
		title : 'homePage',
		autoScroll : true,
		html : '<iframe scrolling="auto" marginHeight=0 marginWidth=0 frameborder="0" width="100%" height="100%" src="Fixed Assessment System.html" ></iframe>'
	}]
})


var buildLayout = function() {
	var layout = new Ext.Viewport({
				layout : 'border',
				items : [{
							region : 'north',
							height : 70,
							contentEl : 'banner'
						}, {
							title : 'Navigation menu',
							region : 'west',
							width : 200,
							split : true,
							collapsible : true,
							maxSize : 200,
							minSize : 200,
							layout : 'accordion',
							defaults : {
								autoScroll : true,
								border : false
							},
							layoutConfig : {
								animate : true
							}
						}, contentPanel, {
							region : 'south',
							height : 50,
							contentEl : 'foot'
						}]
			});

	new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : '/Shrek/getMenu'
						}),
				reader : new Ext.data.JsonReader({}, ['id', 'title']),
				autoLoad : true
			}).on('load', function(store, records) {
		var nav = layout.items.get(1);

		for (var i = 0; i < records.length; i++) {
			var menu = new Ext.Panel({
						id : records[i].data.id,
						title : records[i].data.title,
						iconCls : 'nav'
					});

			var root = new Ext.tree.AsyncTreeNode({
						id : records[i].data.id,
						text : records[i].data.title
					});
			var tree = new Ext.tree.TreePanel({
						rootVisible : false,
						loader : new Ext.tree.TreeLoader({
									dataUrl : '/Shrek/getMenu?tree_menu_id='
											+ records[i].data.id
								}),
						root : root,
						border : false

					});
			tree.on('click', function(node, event) {
				var url = node.attributes.url;
				event.stopEvent();
				var n = contentPanel.getComponent(node.id);
				if (!n) {
					n = contentPanel.add({
						'id' : node.id,
						'title' : node.text,
						closable : true,
						html : '<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src='
								+ url + '></iframe>'
					})
				}
				contentPanel.setActiveTab(n);
			});
			root.expand(true);

			menu.add(tree);
			nav.add(menu);
		}
		layout.doLayout();
	});

}

var userCenter = function() {
	/*
	 * var userRecord = new Ext.data.Record.create([{ name : 'userID' }, { name :
	 * 'userName' }, { name : 'passWord' }, { name : 'trueName' }, { name :
	 * 'phone' }, { name : 'unit' }, { name : 'job' }, { name : 'email' }, {
	 * name : 'workPhone' }, { name : 'mobilePhone' }, { name : 'belongtoRole'
	 * }]) var userStore = new Ext.data.Store({ proxy : new Ext.data.HttpProxy({
	 * url : '/FAM/getUserInfo' }), reader : new
	 * Ext.data.JsonReader({root:'root'}, userRecord) });
	 */
	Ext.Ajax.request({
		url : '/Shrek/getUserInfo',
		success : function(response) {
			var record = Ext.util.JSON.decode(response.responseText);
			userForm.getForm().loadRecord(record);
			userForm.form.findField('userName').setValue(record.userName);
			userForm.form.findField('passWord').setValue(record.passWord);
			userForm.form.findField('trueName').setValue(record.trueName);
			userForm.form.findField('unit').setValue(record.unit);
			userForm.form.findField('job').setValue(record.job);
			userForm.form.findField('email').setValue(record.email);
			userForm.form.findField('workPhone').setValue(record.workPhone);
			userForm.form.findField('mobilePhone').setValue(record.mobilePhone);
			// userForm.form.findField('belongtoRole').setValue(record.belongtoRole);
		},
		failure : Ext.emptyFn
	});

	userWindow.show();

};
var userForm = new Ext.form.FormPanel({
	width : '100%',
	frame : true,
	border : false,
	labelAlign : 'right',
	labelWidht : 40,
	items : [{
		// xtype : 'fieldset',
		layout : 'column',
		// title : '基本设置',
		// border : true,
		items : [{
					columnWidth : .5,
					layout : 'form',
					xtype : 'fieldset',
					border : false,
					autoHeight : true,
					defaultType : 'textfield',
					defaults : {
						width : 150,
						allowBlank : false
					},
					items : [{

								xtype : 'textfield',
								name : 'userName',
								minLength : 5,
								maxLength : 16,
								vtype : 'alphanum',
								fieldLabel : 'userName'
							}, {

								xtype : 'textfield',
								name : 'passWord',
								minLength : 5,
								maxLength : 16,
								vtype : 'alphanum',
								inputType : 'password',
								fieldLabel : 'passWord'

							}, {
								xtype : 'textfield',
								name : 'trueName',
								fieldLabel : 'real name'

							}, {
								fieldLabel : 'department',
								name : 'unit',
								xtype : 'textfield'
							}]
				}, {
					columnWidth : .5,
					layout : 'form',
					xtype : 'fieldset',
					border : false,
					autoHeight : true,
					defaults : {
						width : 150,
						allowBlank : false
					},
					defaultType : 'textfield',
					items : [{

								xtype : 'textfield',
								name : 'job',
								fieldLabel : 'position'

							}, {

								xtype : 'textfield',
								name : 'email',
								vtype : 'email',
								fieldLabel : 'email'

							}, {

								xtype : 'numberfield',
								name : 'workPhone',
								fieldLabel : 'workPhone',
								maxLength : 11,
								allowNegative : false,
								allowDecimals : false

							}, {

								xtype : 'numberfield',
								name : 'mobilePhone',
								maxLength : 11,
								fieldLabel : 'mobilePhone',
								allowNegative : false,
								allowDecimals : false

							}]
				}]
	}]

});
var userWindow = new Ext.Window({
			title : 'Personal Information',
			width : 600,
			height : 180,
			resizable : false,
			closeAction : 'hide',
			items : userForm,
			buttons : [{
				iconCls : 'save',
				text : 'Save',
				handler : function() {
					if (userForm.getForm().isValid()) {
						userForm.getForm().submit({
									url : '/Shrek/updateUserInfo',
									method : 'post',
									success : function(f, action) {
										userWindow.hide();											
//											insertUserLog(1);
											alert('succeed');
									},
									failure : function() {
//										insertUserLog(2);
										alert('failed');
									}
								});
					} else {
						alert('Please do validation');
					}
				}
			}, {
				iconCls : 'empty',
				text : 'Empty',
				handler : function() {
					userForm.getForm().reset();
				}
			}, {
				iconCls : 'back',
				text : 'Return',
				handler : function() {
					userWindow.hide();
				}
			}],
			buttonAlign : 'right'
		});
var initData = function() {
	if(namese == 'guest')
	{
		Ext.get('userCenter').on('click', function() {
			//alert('guest用户无权访问用户中心');
			alert('Permission denied!'); 
		});
	}
	else
	{
		Ext.get('userCenter').on('click', function() {
					userCenter();
				});
	}
	Ext.get('userHelp').on('click', function() {
				alert('Tips:\n1. If the page loading is wrong,please update your browser.\n2. If you login as a guest,only the query operation is available.\n3. Please make sure that deviceType should be "7800","DBlack","Apple"...\n4. The "remove" button in the delete grid aims to remove the item(s) you prepare to delete.\n');
			});
	Ext.get('reLogin').on('click', function() {
				window.location = '/Shrek/interface/home/login.jsp';
			});
	Ext.get('loginOut').on('click', function() {
				window.close();
				return false;
			});
	
//	if(namese == 'guest')
//    {
//		AssetGridPanel.getTopToolbar().items.get('add').hide();
//		AssetGridPanel.getTopToolbar().items.get('delete').hide();  
//		AssetGridPanel.getTopToolbar().items.get('edit').hide();  
//		alert(namese);
//    }
};
var insertUserLog = function(flag) {
	if (flag == 1) {
		Ext.Ajax.request({
					url : '/Shrek/insertLog',
					success : Ext.emptyFn(),
					failure : Ext.emptyFn(),
					params : {
						data : 1,
						type : "个人信息修改",
						obj : "用户"
					}
				});
	} else if (flag == 2) {
		Ext.Ajax.request({
					url : '/Shrek/insertLog',
					success : Ext.emptyFn(),
					failure : Ext.emptyFn(),
					params : {
						data : 0,
						type : "个人信息修改"
					}
				});
	}
}
var init = function() {
	initData();
	buildLayout();
};

Ext.onReady(init);
